<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>添加活动借用申请</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
  <script type="text/javascript">
  //编写自定义JS代码
  </script>
  <style>
  	#borrowDeviceList{
  		width:100%;
  		margin-top:10px;
  	}
  	
  	#borrowDeviceList th{
  		background-color:#ddd;
  		text-align:center;
  	}
  	
  	#borrowDeviceList td{
  		text-align:center;
  	}
  	
  	#borrowDeviceList td a{
  		cursor:pointer;
  	}
  </style>
 </head>
 <body>
  <t:formvalid formid="formobj" dialog="true" layout="table" action="activityBorrowController.do?addApply" tiptype="1" >
		<input id="id" name="id" type="hidden" value=""> 
		<table style=" " cellpadding="0" cellspacing="1" class="formtable">
				<tr>
					<td align="right">
						<label class="Validform_label">
							用户名/学号:
						</label>
					</td>
					<td class="value"> 
						${apply.applyUser.userName }
					</td>
				</tr>	
				<tr>
					<td align="right">
						<label class="Validform_label">
							姓名:
						</label>
					</td>
					<td class="value"> 
                	    ${apply.applyUser.realName }

					</td>
				</tr>	
				<tr>
					<td align="right">
						<label class="Validform_label">
							预约时间:
						</label>
					</td>
					<td class="value"> 
                	    <fmt:formatDate value="${apply.applyDate }" pattern="yyyy-MM-dd HH:mm"/>
					</td>
				</tr>	
				<tr>
					<td align="right">
						<label class="Validform_label">
							使用时间:
						</label>
					</td>
					<td class="value"> 
                	    <fmt:formatDate value="${apply.beginUseDate }" pattern="yyyy-MM-dd HH:mm"/>至
                	    <fmt:formatDate value="${apply.endUseDate }" pattern="yyyy-MM-dd HH:mm"/>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							设备品牌、型号、个数
						</label>
					</td>
					<td class="value" colspan="4">
						  	${apply.deviceInfo}
					</td>
			 
				</tr>
				 
			</table>
		</t:formvalid>
		<div style="text-align: right;margin-top:10px;">
			<button onclick="openScanWindow()">添加设备</button>
		</div>
		<table id="borrowDeviceList">
			<thead>
				<th>设备品牌</th><th>型号</th><th>编号</th><th>操作</th>
			</thead>
		
		</table>
		
		<div style="text-align:center;margin-top:20px;">
			<button type="button" id="but_login"  onclick="doOutStock()" class="btn-big">
                 	 出库
            </button>
 		</div>
 </body>
 <script>
 
 	 var selectedDevice = new Array();
 
 	  function openScanWindow(){
 		  
 		var addurl = 'deviceOutStockController.do?getRfidNo';
  		var api = frameElement.api;
  		$.ajax({
  			url:addurl,
  			async:false,
  			success:function(d){ 
  				
  				if(d == ''){
  					tip('未获取到电子标签信息！');
  					return;
  				}
  				
  				d = $.parseJSON(d); 
  				addDeviceToTable(d); 
  			}
  		});      
 	  } 
 	  
 	  
 	  /**
 	  *添加设备到table中
 	  */
 	  function addDeviceToTable(result){
 		  
 		 for(var i =0;i<selectedDevice.length;i++){
 			 if( result.id == selectedDevice[i].id){
 				tip("设备已经添加到了清单中！");
 				return false;
 			 }
 		  }
 		  
 		  var tr = "<tr><td>"+result.brand+"</td><td>"+result.model+"</td>"+
 		  "<td>"+result.no+"</td><td><a onclick='deleteRow(this,\""+result.id+"\")'>删除</a></td>" 
 		  "</tr>";
 		  $('#borrowDeviceList').append(tr);
 		  
 		 selectedDevice.push(result);
 		 return true;
 	  }
 	  
 	  
 	  function deleteRow(obj,id){
 		  $(obj).parent().parent().remove();
 		  for(var i =0;i<selectedDevice.length;i++){
 			 if( id == selectedDevice[i].id){
 				selectedDevice.splice(i,1);
 				return;
 			 }
 		  }
 	  }
 	  
 	  /**
 	  *  执行出库操作
 	  */
 	  function doOutStock(){
 		  if(selectedDevice.length == 0){
 			  tip("请选择要被出库的设备！");
 			  return;
 		  }
 		  
 		 $.dialog.confirm('确定要将上述设备出库吗?', function(r) {
			 if(r) { 
		 		  var selectedIds = "";
		 		  for(var i =0;i<selectedDevice.length;i++){
		 			 selectedIds += (selectedDevice[i].id+",");
		 		  }
		 		  
		 		  $.ajax({
		 			  url:'deviceOutStockController.do?activityOutStock&id=${apply.id}&ids='+selectedIds,
		 			  async:false,
		 			  type:'post',
		 			  contentType: 'application/json',
		 			  success:function(d){
		 				  d = $.parseJSON(d);
		 				 if(d.success){
		 					window.top.optSuccess = true;
		 					frameElement.api.close();
		 				 }else{
		 					 tip(d.msg);
		 				 } 
		 			  }
		 		  });
		 		  
			 }
		 });
 		  
 		 return false; 
 	  }
 	  
 </script>
 